package com.tenebrous.jestic.service.framwork.system;

import com.tenebrous.jestic.entity.TreeSerializable;
import com.tenebrous.jestic.entity.framwork.system.MetaDictionary;
import com.tenebrous.jestic.mapper.idao.framwork.system.MetaDictionaryMapper;
import com.tenebrous.jestic.service.SuperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MetaDictionaryService extends SuperService<MetaDictionary> {

    @Autowired
    private MetaDictionaryMapper mapper;

    @Override
    @Autowired
    protected void setSuperMapper() {
        super.setSuperMapper(this.mapper);
    }

    /**
     * 数据查询
     * @return
     */
    public List<TreeSerializable> queryTree() {
        List<TreeSerializable> tlist = new ArrayList<>();
        List<MetaDictionary> mlist = this.mapper.queryAll();
        tlist.addAll(mlist);
        return this.buildTree(tlist, "0");
    }

    public List<MetaDictionary> queryChildListByCode(String code) {
        MetaDictionary entity = this.mapper.getByCode(code);
        if(entity != null) {
            List<MetaDictionary> list = this.mapper.queryList(entity.getId());
            return list;
        }
        return new ArrayList<MetaDictionary>();
    }

    public List<Map<String, Object>> queryChildMapListByCode(String code) {
        MetaDictionary entity = this.mapper.getByCode(code);
        if(entity != null) {
            return this.mapper.queryMapList(entity.getId());
        }
        return new ArrayList<Map<String, Object>>();
    }

    /**
     * 数据查询
     * @return
     */
    public MetaDictionary queryByCode(String code) {
        MetaDictionary entity = this.mapper.getByCode(code);
        return entity;
    }
}
